package com.zhongshengnetwork.rightbe.lang.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhongshengnetwork.rightbe.Constant.Constant;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.customview.ActionSheetDialog;
import com.zhongshengnetwork.rightbe.dbservice.NotifydbService;

import java.util.ArrayList;
import java.util.List;

public class MsgHomeFragment extends BaseAnalyticsFragment implements View.OnClickListener{

    private View rootView;
    private ViewPager mPaper;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = null;
    private TextView tv_comment,tv_care,tv_like,tv_notify;
    private LinearLayout tv_comment_line,tv_care_line,tv_like_line,tv_notify_line;
    public MainActivity mainActivity;
    private MsgFragment commentFragment;
    private MsgFragment careFragment;
    private MsgFragment likeFragment;
    private MsgFragment notifyFragment;
    private int currentPosition = 0;

    public void afterLogin(){
        if (tv_comment == null || tv_comment_line == null || tv_care == null || tv_care_line == null || tv_like == null || tv_like_line == null
                || tv_notify == null || tv_notify_line == null ){
            return;
        }
        upateTitle(false);
        if (commentFragment != null){
            commentFragment.getData();
        }
        if (careFragment != null){
            careFragment.getData();
        }
        if (likeFragment != null){
            likeFragment.getData();
        }
        if (notifyFragment != null){
            notifyFragment.getData();
        }
    }

    public void selectUpate(){

        if (tv_comment == null || tv_comment_line == null || tv_care == null || tv_care_line == null || tv_like == null || tv_like_line == null
                || tv_notify == null || tv_notify_line == null ){
            return;
        }

        if (mFragments == null || mFragments.size() == 0){
            return;
        }

        int index = upateTitle(false);
        if (index >= 0){
            if (mFragments.size() > index && index != mPaper.getCurrentItem()){
                resetColor();
                setSelect(index);
            }

        }
    }

    public void updateTitleByType(int type){

        if (tv_comment == null || tv_comment_line == null || tv_care == null || tv_care_line == null || tv_like == null || tv_like_line == null
                || tv_notify == null || tv_notify_line == null ){
            return;
        }

        long count = 0;
        if (type == Constant.MessageListType.CommentMsgType){
            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.CommentMsgType);
            if (count > 0){
                if (count > 99){
                    tv_comment.setText("评论(99+)");
                }else {
                    tv_comment.setText("评论("+count+")");
                }
            }else {
                tv_comment.setText("评论");
            }
        }else if (type == Constant.MessageListType.CareMsgType){
            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.CareMsgType);
            if (count > 0){
                if (count > 99){
                    tv_care.setText("关注(99+)");
                }else {
                    tv_care.setText("关注("+count+")");
                }
            }else {
                tv_care.setText("关注");
            }
        }else if (type == Constant.MessageListType.DefaultMsgType){
            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.DefaultMsgType);
            if (count > 0){
                if (count > 99){
                    tv_like.setText("点赞(99+)");
                }else {
                    tv_like.setText("点赞("+count+")");
                }
            }else {
                tv_like.setText("点赞");
            }
        }else if (type == Constant.MessageListType.NotifyMsgType){
            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.NotifyMsgType);
            if (count > 0){
                if (count > 99){
                    tv_notify.setText("通知(99+)");
                }else {
                    tv_notify.setText("通知("+count+")");
                }
            }else {
                tv_notify.setText("通知");
            }
        }
    }

    public void getLatest(){
        upateTitle(false);
        if (commentFragment != null){
            commentFragment.getLatest();
        }
        if (careFragment != null){
            careFragment.getLatest();
        }
        if (likeFragment != null){
            likeFragment.getLatest();
        }
        if (notifyFragment != null){
            notifyFragment.getLatest();
        }
    }


    public void goToTop(){
        if (currentPosition == 0){
            if (commentFragment != null){
                commentFragment.goToTop();
            }
        }else if (currentPosition == 1){
            if (careFragment != null){
                careFragment.goToTop();
            }
        }else if (currentPosition == 2){
            if (likeFragment != null){
                likeFragment.goToTop();
            }
        }else if (currentPosition == 3){
            if (notifyFragment != null){
                notifyFragment.goToTop();
            }
        }

    }

    public void allToRead(){

        upateTitle(true);

        if (commentFragment != null){
            commentFragment.dealOp("全部标注为已读");
        }

        if (careFragment != null){
            careFragment.dealOp("全部标注为已读");
        }

        if (likeFragment != null){
            likeFragment.dealOp("全部标注为已读");
        }

        if (notifyFragment != null){
            notifyFragment.dealOp("全部标注为已读");
        }

        if (mainActivity != null){
            mainActivity.img_message_count.setVisibility(View.INVISIBLE);
        }

    }

    public void allToDelete(){

        upateTitle(true);

        if (commentFragment != null){
            commentFragment.dealOp("删除全部");
        }

        if (careFragment != null){
            careFragment.dealOp("删除全部");
        }

        if (likeFragment != null){
            likeFragment.dealOp("删除全部");
        }

        if (notifyFragment != null){
            notifyFragment.dealOp("删除全部");
        }

        if (mainActivity != null){
            mainActivity.img_message_count.setVisibility(View.INVISIBLE);
        }

    }

    public void showMenu(View view){
        if (getActivity() == null){
            return;
        }
        List<String> names = new ArrayList<String>();
        names.add("全部标注为已读");
        names.add("删除全部");
        ActionSheetDialog actionSheetDialog = new ActionSheetDialog(getActivity())
                .builder()
                .setTitle("请选择")
                .setCancelable(false)
                .setCanceledOnTouchOutside(true);
        for (String s : names){
            actionSheetDialog.addSheetItem(s, null, new ActionSheetDialog.OnSheetItemClickListener() {
                @Override
                public void onClick(int which) {
                    if (which > 0){
                        String title = names.get(which-1);
                        if (title.equals("全部标注为已读")){
                            allToRead();
                        }else if (title.equals("删除全部")){
                            allToDelete();
                        }
                    }
                }
            });
        }
        actionSheetDialog.show();
    }

    public void updateSXTip(int count){
        if (notifyFragment != null){
            notifyFragment.updateSXTip(count);
        }
    }

    public int upateTitle(boolean isClear){

        if (tv_comment == null || tv_comment_line == null || tv_care == null || tv_care_line == null || tv_like == null || tv_like_line == null
                || tv_notify == null || tv_notify_line == null ){
            return 0;
        }

        boolean flag = false;
        int index = -1;

        if (CommonUtils.isLogin() == true && isClear == false){
            long count = 0;

            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.CommentMsgType);
            if (count > 0){
                index = 0;
                flag = true;
                if (count > 99){
                    tv_comment.setText("评论(99+)");
                }else {
                    tv_comment.setText("评论("+count+")");
                }
            }else {
                tv_comment.setText("评论");
            }

            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.CareMsgType);
            if (count > 0){
                if (flag == false){
                    flag = true;
                    index = 1;
                }
                if (count > 99){
                    tv_care.setText("关注(99+)");
                }else {
                    tv_care.setText("关注("+count+")");
                }
            }else {
                tv_care.setText("关注");
            }

            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.DefaultMsgType);
            if (count > 0){
                if (flag == false){
                    flag = true;
                    index = 2;
                }
                if (count > 99){
                    tv_like.setText("点赞(99+)");
                }else {
                    tv_like.setText("点赞("+count+")");
                }
            }else {
                tv_like.setText("点赞");
            }

            count = NotifydbService.getUnReadCountByType(CustomApplication.loginModel.getUid(), Constant.MessageListType.NotifyMsgType);
            if (count > 0){
                if (flag == false){
                    flag = true;
                    index = 3;
                }
                if (count > 99){
                    tv_notify.setText("通知(99+)");
                }else {
                    tv_notify.setText("通知("+count+")");
                }
            }else {
                tv_notify.setText("通知");
            }

        }else {
            tv_comment.setText("评论");
            tv_care.setText("关注");
            tv_like.setText("点赞");
            tv_notify.setText("通知");
        }
        return index;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //System.out.print("首页界面创建\n");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.msg_home_layout, container,false);
            initLayout();
            if (mAdapter == null){
                mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
                    @Override
                    public int getCount() {
                        return mFragments.size();
                    }

                    @Override
                    public Fragment getItem(int arg0) {
                        return mFragments.get(arg0);
                    }


                };
            }
            mPaper.setAdapter(mAdapter);
            mPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                private int currentIndex = 0;

                @Override
                public void onPageSelected(int position) {
                    resetColor();
                    setSelect(position);
                    currentIndex = position;
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });

            LinearLayout category_layout = (LinearLayout) rootView.findViewById(R.id.category_layout);
            category_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }


    private void setSelect(int position){
        if (tv_comment == null || tv_comment_line == null || tv_care == null || tv_care_line == null || tv_like == null || tv_like_line == null
                || tv_notify == null || tv_notify_line == null ){
            return;
        }
        currentPosition = position;
        switch (position) {
            case 0:
                commentFragment.getFirstData();
                tv_comment.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_comment_line.setVisibility(View.VISIBLE);
                if (mPaper.getCurrentItem() != 0){
                    mPaper.setCurrentItem(0);
                }

                break;
            case 1://FC6E51
                careFragment.getFirstData();
                tv_care.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_care_line.setVisibility(View.VISIBLE);
                if (mPaper.getCurrentItem() != 1){
                    mPaper.setCurrentItem(1);
                }
                break;
            case 2:
                likeFragment.getFirstData();
                tv_like.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_like_line.setVisibility(View.VISIBLE);
                if (mPaper.getCurrentItem() != 2){
                    mPaper.setCurrentItem(2);
                }
                break;
            case 3:
                notifyFragment.getFirstData();
                tv_notify.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_notify_line.setVisibility(View.VISIBLE);
                if (mPaper.getCurrentItem() != 3){
                    mPaper.setCurrentItem(3);
                }
                break;
            default:
                tv_comment.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_comment_line.setVisibility(View.VISIBLE);
                if (mPaper.getCurrentItem() != 0){
                    mPaper.setCurrentItem(0);
                }
                break;
        }
    }

    /**
     * 初始化控件
     */
    public void initLayout(){
        tv_comment = (TextView)rootView.findViewById(R.id.tv_comment);
        tv_comment_line = (LinearLayout) rootView.findViewById(R.id.tv_comment_line);

        tv_care = (TextView)rootView.findViewById(R.id.tv_care);
        tv_care_line = (LinearLayout) rootView.findViewById(R.id.tv_care_line);

        tv_like = (TextView)rootView.findViewById(R.id.tv_like);
        tv_like_line = (LinearLayout) rootView.findViewById(R.id.tv_like_line);

        tv_notify = (TextView)rootView.findViewById(R.id.tv_notify);
        tv_notify_line = (LinearLayout) rootView.findViewById(R.id.tv_notify_line);

        mPaper = (ViewPager)rootView.findViewById(R.id.view_pager);

        tv_comment.setOnClickListener(this);
        tv_care.setOnClickListener(this);
        tv_like.setOnClickListener(this);
        tv_notify.setOnClickListener(this);

        int index = upateTitle(false);




        if (mFragments == null){

            mFragments = new ArrayList<Fragment>();

            commentFragment = new MsgFragment();
            commentFragment.dataType = Constant.MessageListType.CommentMsgType;
            commentFragment.msgHomeFragment = this;
            mFragments.add(commentFragment);

            careFragment = new MsgFragment();
            careFragment.dataType = Constant.MessageListType.CareMsgType;
            careFragment.msgHomeFragment = this;
            mFragments.add(careFragment);

            likeFragment = new MsgFragment();
            likeFragment.dataType = Constant.MessageListType.DefaultMsgType;
            likeFragment.msgHomeFragment = this;
            mFragments.add(likeFragment);

            notifyFragment = new MsgFragment();
            notifyFragment.dataType = Constant.MessageListType.NotifyMsgType;
            notifyFragment.msgHomeFragment = this;
            mFragments.add(notifyFragment);

        }

        if (index < 0){
            index = 0;
        }
        resetColor();
        setSelect(index);


    }

    public void resetColor(){

        if (tv_comment == null || tv_comment_line == null || tv_care == null || tv_care_line == null || tv_like == null || tv_like_line == null
                || tv_notify == null || tv_notify_line == null ){
            return;
        }

        tv_comment.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_comment_line.setVisibility(View.INVISIBLE);

        tv_care.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_care_line.setVisibility(View.INVISIBLE);

        tv_like.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_like_line.setVisibility(View.INVISIBLE);

        tv_notify.setTextColor(AppThemeUtils.getInstance().getTextColorOfWeiJu());
        tv_notify_line.setVisibility(View.INVISIBLE);

    }
    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.tv_comment:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
            case R.id.tv_care:
                resetColor();
                setSelect(1);
                mPaper.setCurrentItem(1);
                break;
            case R.id.tv_like:
                resetColor();
                setSelect(2);
                mPaper.setCurrentItem(2);
                break;
            case R.id.tv_notify:
                resetColor();
                setSelect(3);
                mPaper.setCurrentItem(3);
                break;

            default:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
        }
    }
    /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        public List<Activity> mListViews;

        public MyPagerAdapter(List<Activity> mListViews) {
            this.mListViews = mListViews;
        }


        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }



        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

}

